package com.zhiyou100.api.date_demo;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Demo2 {

    public static void main(String[] args) {
        testDateFormat();

    }

    // ? DateFormat 不涉及日历  如果一个日历对象转String  怎么办?






    //DateFormat  进行日期格式化  1、Date --->String  2、String--->Date
    public static void testDateFormat(){
        Date date = new Date();
        //创建DateFormat对象   指定转换格式  以及 日期地区
        DateFormat df1 = DateFormat.getDateInstance(DateFormat.SHORT, Locale.CHINA);
        DateFormat df2 = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.CHINA);
        DateFormat df3 = DateFormat.getDateInstance(DateFormat.LONG, Locale.CHINA);
        DateFormat df4 = DateFormat.getDateInstance(DateFormat.FULL, Locale.CHINA);
        String s = df1.format(new Date());
        System.out.println("s = " + s);
        String s1 = df2.format(new Date());
        System.out.println("s1 = " + s1);
        String s2 = df3.format(new Date());
        System.out.println("s2 = " + s2);
        String s3 = df4.format(new Date());
        System.out.println("s3 = " + s3);
        //格式时间getTimeInstance
        DateFormat tis = DateFormat.getTimeInstance(DateFormat.DEFAULT, Locale.CHINA);
        String s4 = tis.format(date);
        System.out.println("s4 = " + s4);
    }


    //日历的获取
    public static void test() {
        //获取一个默认时区的日历对象
        Calendar calendar = Calendar.getInstance();
        System.out.println("calendar = " + calendar);
        //将日历传成Date对象
        Date date = calendar.getTime();
        System.out.println("date = " + date);
        //根据Date设定一个日历
        calendar.setTime(new Date());
        //获取日历字段中的年 月  日   时  分  秒
        int weekYear = calendar.getWeekYear();
        System.out.println("weekYear = " + weekYear);
        int year = calendar.get(Calendar.YEAR);
        System.out.println("year = " + year);
        int month = calendar.get(Calendar.MONTH);
        System.out.println("month = " + month);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println("day = " + day);
        //获取星期几
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        System.out.println("week = " + week);
        //获取某个字段的最大值
        //获取这个月有多少天
        int max_day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        System.out.println("max_day = " + max_day);
        //获取当月最多多少周
        int week_of_month = calendar.getActualMaximum(Calendar.WEEK_OF_MONTH);
        System.out.println("week_of_month = " + week_of_month);


        //修改日历的某个字段
//        calendar.set(Calendar.YEAR, 2021);
        //修改年月日
        calendar.set(2022,1,19);
        System.out.println(calendar.getTime());

    }


}
